package cn.iocoder.yudao.module.search.controller.admin.search;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.search.api.search.dto.*;
import cn.iocoder.yudao.module.search.service.search.SearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 搜索管理
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 搜索管理")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
@Validated
public class SearchController {

    private final SearchService searchService;

    // ========== 索引管理 ==========

    @PostMapping("/index/create")
    @Operation(summary = "创建索引")
    @PreAuthorize("@ss.hasPermission('search:index:create')")
    public CommonResult<Boolean> createIndex(@Valid @RequestBody SearchIndexReqDTO reqDTO) {
        Boolean result = searchService.createIndex(reqDTO);
        return success(result);
    }

    @DeleteMapping("/index/delete")
    @Operation(summary = "删除索引")
    @Parameter(name = "indexName", description = "索引名称", example = "user_index", required = true)
    @PreAuthorize("@ss.hasPermission('search:index:delete')")
    public CommonResult<Boolean> deleteIndex(@RequestParam("indexName") String indexName) {
        Long tenantId = TenantContextHolder.getTenantId();
        Boolean result = searchService.deleteIndex(tenantId, indexName);
        return success(result);
    }

    @GetMapping("/index/exists")
    @Operation(summary = "检查索引是否存在")
    @Parameter(name = "indexName", description = "索引名称", example = "user_index", required = true)
    @PreAuthorize("@ss.hasPermission('search:index:query')")
    public CommonResult<Boolean> existsIndex(@RequestParam("indexName") String indexName) {
        Long tenantId = TenantContextHolder.getTenantId();
        Boolean result = searchService.existsIndex(tenantId, indexName);
        return success(result);
    }

    @GetMapping("/index/get")
    @Operation(summary = "获取索引信息")
    @Parameter(name = "indexName", description = "索引名称", example = "user_index", required = true)
    @PreAuthorize("@ss.hasPermission('search:index:query')")
    public CommonResult<SearchIndexRespDTO> getIndex(@RequestParam("indexName") String indexName) {
        Long tenantId = TenantContextHolder.getTenantId();
        SearchIndexRespDTO result = searchService.getIndex(tenantId, indexName);
        return success(result);
    }

    @GetMapping("/index/list")
    @Operation(summary = "获取所有索引列表")
    @PreAuthorize("@ss.hasPermission('search:index:query')")
    public CommonResult<List<SearchIndexRespDTO>> listIndexes() {
        Long tenantId = TenantContextHolder.getTenantId();
        List<SearchIndexRespDTO> result = searchService.listIndexes(tenantId);
        return success(result);
    }

    // ========== 文档管理 ==========

    @PostMapping("/document/create")
    @Operation(summary = "创建文档")
    @PreAuthorize("@ss.hasPermission('search:document:create')")
    public CommonResult<String> createDocument(@Valid @RequestBody SearchDocumentReqDTO reqDTO) {
        String result = searchService.createDocument(reqDTO);
        return success(result);
    }

    @PutMapping("/document/update")
    @Operation(summary = "更新文档")
    @PreAuthorize("@ss.hasPermission('search:document:update')")
    public CommonResult<Boolean> updateDocument(@Valid @RequestBody SearchDocumentReqDTO reqDTO) {
        Boolean result = searchService.updateDocument(reqDTO);
        return success(result);
    }

    @DeleteMapping("/document/delete")
    @Operation(summary = "删除文档")
    @Parameter(name = "indexName", description = "索引名称", example = "user_index", required = true)
    @Parameter(name = "documentId", description = "文档ID", example = "1", required = true)
    @PreAuthorize("@ss.hasPermission('search:document:delete')")
    public CommonResult<Boolean> deleteDocument(@RequestParam("indexName") String indexName,
                                               @RequestParam("documentId") String documentId) {
        Long tenantId = TenantContextHolder.getTenantId();
        Boolean result = searchService.deleteDocument(tenantId, indexName, documentId);
        return success(result);
    }

    @GetMapping("/document/get")
    @Operation(summary = "获取文档")
    @Parameter(name = "indexName", description = "索引名称", example = "user_index", required = true)
    @Parameter(name = "documentId", description = "文档ID", example = "1", required = true)
    @PreAuthorize("@ss.hasPermission('search:document:query')")
    public CommonResult<SearchDocumentRespDTO> getDocument(@RequestParam("indexName") String indexName,
                                                          @RequestParam("documentId") String documentId) {
        Long tenantId = TenantContextHolder.getTenantId();
        SearchDocumentRespDTO result = searchService.getDocument(tenantId, indexName, documentId);
        return success(result);
    }

    @PostMapping("/document/bulk")
    @Operation(summary = "批量操作文档")
    @PreAuthorize("@ss.hasPermission('search:document:create')")
    public CommonResult<SearchBulkRespDTO> bulkDocument(@Valid @RequestBody SearchBulkReqDTO reqDTO) {
        SearchBulkRespDTO result = searchService.bulkDocument(reqDTO);
        return success(result);
    }

    // ========== 搜索查询 ==========

    @PostMapping("/query")
    @Operation(summary = "搜索查询")
    @PreAuthorize("@ss.hasPermission('search:query:execute')")
    public CommonResult<SearchQueryRespDTO> search(@Valid @RequestBody SearchQueryReqDTO reqDTO) {
        SearchQueryRespDTO result = searchService.search(reqDTO);
        return success(result);
    }

}
